<template>
	<div class="system">
		<el-form :model="config" label-width="100px" ref="form" class="system">
			<h2>系统配置</h2>
			<el-form-item label="环境值">
				 <el-radio-group v-model="config.system.env">
					 <el-radio label="develop">开发</el-radio>
					 <el-radio label="production">生产</el-radio>
					 <el-radio label="public">演示</el-radio>
				  </el-radio-group>
			</el-form-item>
			<el-form-item label="端口值">
				<el-input v-model.number="config.system.addr"></el-input>
			</el-form-item>
			<el-form-item label="数据库类型">
				<el-select v-model="config.system.dbType">
					<el-option label="MYSQL" value="mysql"></el-option>
					<el-option label="POSTRESQL" value="postgresql"></el-option>
					<el-option label="SQLSERVER" value="sqlserver"></el-option>
					<el-option label="SQLITE" value="sqlite"></el-option>
				</el-select>
			</el-form-item>
		   <el-form-item label="Oss类型">
				<el-select v-model="config.system.ossType">
				  <el-option value="local" label="本地"></el-option>
				  <el-option value="qiniu" label="七牛云"></el-option>
				</el-select>
			</el-form-item>
		   <el-form-item label="配置文件">
				<el-input v-model.number="config.system.configEnv"></el-input>
		   </el-form-item>
			<el-form-item label="多点登录拦截">
				<el-switch v-model="config.system.useMultipoint" active-text="开启" inactive-text="关闭"></el-switch>
			</el-form-item>
			  <!--  System end  -->
			
			<h2>jwt签名</h2>
			<el-form-item label="jwt签名">
				<el-input v-model="config.jwt.signingKey"></el-input>
			</el-form-item>
			
			<h2>casbin配置</h2>
			<el-form-item label="模型地址">
				<el-input v-model="config.casbin.modelPath"></el-input>
			</el-form-item>
			
			<template v-if="config.system.dbType == 'mysql'">
				<h2>Mysql 数据库配置</h2>
				<el-form-item label="用户名">
					<el-input v-model="config.mysql.username"></el-input>
				</el-form-item>
				<el-form-item label="密码">
					<el-input v-model="config.mysql.password"></el-input>
				</el-form-item>
				<el-form-item label="连接">
					<el-input v-model="config.mysql.path"></el-input>
				</el-form-item>
				<el-form-item label="数据库名称">
					<el-input v-model="config.mysql.dbname"></el-input>
				</el-form-item>
				<el-form-item label="空闲连接数">
					<el-input v-model.number="config.mysql.maxIdleConns"></el-input>
				</el-form-item>
				<el-form-item label="最大连接数">
					<el-input v-model.number="config.mysql.maxOpenConns"></el-input>
				</el-form-item>
				<el-form-item label="日志打印">
					<el-switch v-model="config.log.logMode"></el-switch>
				</el-form-item>
			</template>
			<template v-if="config.system.dbType == 'postgresql'">
				<h2>Postgresql 数据库配置</h2>
				<el-form-item label="用户名">
					<el-input v-model="config.postgresql.username"></el-input>
				</el-form-item>
				<el-form-item label="密码">
					<el-input v-model="config.postgresql.password"></el-input>
				</el-form-item>
				<el-form-item label="数据库名称">
					<el-input v-model="config.postgresql.dbname"></el-input>
				</el-form-item>
				<el-form-item label="端口">
					<el-input v-model="config.postgresql.port"></el-input>
				</el-form-item>
				<el-form-item label="空闲连接数">
					<el-input v-model.number="config.postgresql.maxIdleConns"></el-input>
				</el-form-item>
				<el-form-item label="最大连接数">
					<el-input v-model.number="config.postgresql.maxOpenConns"></el-input>
				</el-form-item>
				<el-form-item label="日志打印">
					<el-switch v-model="config.postgresql.logger"></el-switch>
				</el-form-item>
			</template>
			<template v-if="config.system.dbType == 'sqlite'">
				<h2>Sqlite 数据库配置</h2>
				<el-form-item label="连接">
					<el-input v-model="config.sqlite.path"></el-input>
				</el-form-item>
				<el-form-item label="空闲连接数">
					<el-input v-model.number="config.sqlite.maxIdleConns"></el-input>
				</el-form-item>
				<el-form-item label="最大连接数">
					<el-input v-model.number="config.sqlite.maxOpenConns"></el-input>
				</el-form-item>
				<el-form-item label="打印日志">
					<el-switch v-model="config.sqlite.logger"></el-switch>
				</el-form-item>
			</template>
			<template v-if="config.system.dbType == 'sqlserver'">
				<h2>Sqlserver 数据库配置</h2>
				<el-form-item label="用户名">
					<el-input v-model="config.sqlserver.username"></el-input>
				</el-form-item>
				<el-form-item label="密码">
					<el-input v-model="config.sqlserver.password"></el-input>
				</el-form-item>
				<el-form-item label="连接">
					<el-input v-model="config.sqlserver.path"></el-input>
				</el-form-item>
				<el-form-item label="数据库名称">
					<el-input v-model="config.sqlserver.dbname"></el-input>
				</el-form-item>
				<el-form-item label="空闲连接数">
					<el-input v-model.number="config.sqlserver.maxIdleConns"></el-input>
				</el-form-item>
				<el-form-item label="最大连接数">
					<el-input v-model.number="config.sqlserver.maxOpenConns"></el-input>
				</el-form-item>
				<el-form-item label="打印日志">
					<el-switch v-model="config.sqlserver.logger"></el-switch>
				</el-form-item>
			</template>
			
			<h2>Redis 数据库配置</h2>
			<el-form-item label="地址">
				<el-input v-model="config.redis.addr"></el-input>
			</el-form-item>
			<el-form-item label="密码">
				<el-input v-model="config.redis.password"></el-input>
			</el-form-item>
			<el-form-item label="数据库序号">
				<el-input v-model="config.redis.db"></el-input>
			</el-form-item>
			
			<!--  Zap start  -->
			  <h2>Zap日志配置</h2>
			  <el-form-item label="级别">
				<el-input v-model.number="config.zap.level"></el-input>
			  </el-form-item>
			  <el-form-item label="输出">
				<el-input v-model="config.zap.format"></el-input>
			  </el-form-item>
			  <el-form-item label="日志前缀">
				<el-input v-model="config.zap.prefix"></el-input>
			  </el-form-item>
			  <el-form-item label="日志文件夹">
				<el-input v-model="config.zap.director"></el-input>
			  </el-form-item>
			  <el-form-item label="软链接名称">
				<el-input v-model="config.zap.linkName"></el-input>
			  </el-form-item>
			  <el-form-item label="编码级">
				<el-input v-model="config.zap.encodeLevel"></el-input>
			  </el-form-item>
			  <el-form-item label="栈名">
				<el-input v-model="config.zap.stacktraceKey"></el-input>
			  </el-form-item>
			  <el-form-item label="显示行">
				<el-checkbox v-model="config.zap.showLine"></el-checkbox>
			  </el-form-item>
			  <el-form-item label="输出控制台">
				<el-checkbox v-model="config.zap.logInConsole"></el-checkbox>
			  </el-form-item>
			  <!--  Zap end  -->

			  <!--  Email start  -->
			  <h2>邮箱配置</h2>
			  <el-form-item label="接收者邮箱">
				<el-input v-model="config.email.to" placeholder="可多个，以逗号分隔"></el-input>
			  </el-form-item>
			  <el-form-item label="端口">
				<el-input v-model.number="config.email.port"></el-input>
			  </el-form-item>
			  <el-form-item label="发送者邮箱">
				<el-input v-model="config.email.from"></el-input>
			  </el-form-item>
			  <el-form-item label="host">
				<el-input v-model="config.email.host"></el-input>
			  </el-form-item>
			  <el-form-item label="是否为ssl">
				<el-checkbox v-model="config.email.isSSL"></el-checkbox>
			  </el-form-item>
			  <el-form-item label="secret">
				<el-input v-model="config.email.secret"></el-input>
			  </el-form-item>
			  <el-form-item label="测试邮件">
				<el-button @click="email">测试邮件</el-button>
			  </el-form-item>
			  <!--  Email end  -->

			<h2>本地上传配置</h2>
				<!-- <el-form-item label="本地或七牛云"> -->
					<!-- <el-checkbox v-model="config.local.local">本地</el-checkbox> -->
				<!-- </el-form-item> -->
				<el-form-item label="本地头像路径">
					<el-input v-model="config.local.avatarPath"></el-input>
				</el-form-item>
				<el-form-item label="本地文件路径">
					<el-input v-model="config.local.filePath"></el-input>
				</el-form-item>
			<h2>七牛云配置</h2>
			<el-form-item label="存储区域">
			<el-input v-model="config.qiniu.zone"></el-input>
			</el-form-item>
			<el-form-item label="空间名称">
			  <el-input v-model="config.qiniu.bucket"></el-input>
			</el-form-item>
			<el-form-item label="CDN加速域名">
			  <el-input v-model="config.qiniu.imgPath"></el-input>
			</el-form-item>
			<el-form-item label="是否使用https">
			  <el-checkbox v-model="config.qiniu.imgPath">开启</el-checkbox>
			</el-form-item>
			<el-form-item label="accessKey">
			  <el-input v-model="config.qiniu.accessKey"></el-input>
			</el-form-item>
			<el-form-item label="secretKey">
			  <el-input v-model="config.qiniu.secretKey"></el-input>
			</el-form-item>
			<el-form-item label="上传是否使用CDN上传加速">
			  <el-checkbox v-model="config.qiniu.useCdnDomains">开启</el-checkbox>
			</el-form-item>
			</el-form-item>
			
			<h2>验证码配置</h2>
			<el-form-item label="验证码位数">
				<el-input v-model.number="config.captcha.keyLong"></el-input>
			</el-form-item>
			<el-form-item label="图片宽度">
				<el-input v-model.number="config.captcha.imgWidth"></el-input>
			</el-form-item>
			<el-form-item label="图片高度">
				<el-input v-model.number="config.captcha.imgHeight"></el-input>
			</el-form-item>
			
			<h2>go-logging日志配置</h2>
			<el-form-item label="前缀">
				<el-input v-model.number="config.log.prefix"></el-input>
			</el-form-item>
			<el-form-item label="输出到文件">
				<el-switch v-model="config.log.logFile"></el-switch>
			</el-form-item>
			<el-form-item>
				<el-button @click="update" type="primary">立即更新</el-button>
				<el-button @click="reload" type="primary">重启服务（开发中）</el-button>
			</el-form-item>
		</el-form>
	</div>
</template>

<script>
import { getSystemConfig, setSystemConfig } from "@/api/system";
import { emailTest } from "@/api/email";
export default {
  name: "Config",
  data() {
    return {
      config: {
        system: {},
        jwt: {},
        casbin: {},
        mysql: {},
		postgresql:{},
		sqlserver: {},
        sqlite: {},
        redis: {},
        qiniu: {},
        captcha: {},
		log: {},
		zap: {},
		local: {}
      }
    };
  },
  async created() {
    await this.initForm();
  },
  methods: {
    async initForm() {
      const res = await getSystemConfig();
      if (res.code == 0) {
        this.config = res.data.config;
      }
    },
    reload() {},
    async update() {
      const res = await setSystemConfig({ config: this.config });
      if (res.code == 0) {
        this.$message({
          type: "success",
          message: "配置文件设置成功"
        });
        await this.initForm();
      }
    },
    async email() {
        const res = await emailTest();
        if (res.code == 0) {
          this.$message({
            type: "success",
            message: "邮件发送成功"
          });
          await this.initForm();
        } else {
          this.$message({
            type: "error",
            message: "邮件发送失败"
          });
        }
      }
  }
};
</script>
<style lang="scss">
.system {
  h2 {
    padding: 10px;
    margin: 10px 0;
    font-size: 16px;
    box-shadow: -4px 1px 3px 0px #e7e8e8;
  }
}
</style>
